package com.hellobike.haxing.modules.supplier.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by zhanglei on 2020/1/15.
 */
public class Convert {

    public static String list2Json(List<String> list){
        if(list == null || list.size() < 1){
            return null;
        }
        return JSON.toJSONString(list);
    }

    public static List<String> json2list(String json){
        if(json == null || json.isEmpty()) {
            return null;
        }
        return JSONObject.parseArray(json, String.class);
    }

    public static String getOrderCodeByTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="OC_";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        return result+newDate;
    }

    public static String getSendCodeByTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="SD_";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        return result+newDate;
    }

    public static String getBatchCodeByTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="BC_";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        return result+newDate;
    }

    public static void main(String[] args) throws UnknownHostException {
        List<String> lt =new ArrayList<>();
        lt.add("aa");
        lt.add("bb");
        System.out.println(list2Json(lt));

        String lr = "[\"aa\",\"bb\"]";
        List<String> le =new ArrayList<>();
        le = json2list(lr);
        System.out.println(le);
    }
}
